package com.dji.ux.sample.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dji.ux.sample.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PanoTaskAdapter extends RecyclerView.Adapter<PanoTaskAdapter.PanoTaskHolder> {

    private String TAG = getClass().getName();
    private Context context;
    private List<PanoTaskBean> dataList = new ArrayList<>();

    public PanoTaskAdapter(Context context, List<PanoTaskBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void refreshData(List<PanoTaskBean> data) {
        this.dataList.clear();
        this.dataList.addAll(data);
        this.notifyDataSetChanged();
    }

    public void updatePanoProress(int position, int progress, int shoot_num) {
        if (!dataList.isEmpty() && position < dataList.size() && position >= 0) {
            if (!(this.dataList.get(position).getPano_progress() == 0 && progress == 100))
                this.dataList.get(position).setPano_progress(progress);
            if (this.dataList.get(position).getShoot_num() != 25)
                this.dataList.get(position).setShoot_num(shoot_num);
            this.notifyItemChanged(position, 1);
        }
    }

    public void updateSaveProress(int position, int progress) {
        if (!dataList.isEmpty() && dataList.size() > position && position >= 0) {
            this.dataList.get(position).setSave_progress(progress);
            this.notifyItemChanged(position, 1);
        }
    }

    public void updateSavePath(int position, String path) {
        if (!dataList.isEmpty() && dataList.size() > position && position >= 0) {
            this.dataList.get(position).setSave_path(path);
            this.notifyItemChanged(position, 1);
        }
    }

    public void updateUploadProgress(int position, int progress) {
        if (!dataList.isEmpty() && dataList.size() > position && position >= 0) {
            this.dataList.get(position).setUpload_progress(progress);
            this.notifyItemChanged(position, 1);
        }
    }

    public void updatePanoStatus(int position, String status) {
        if (!dataList.isEmpty() && dataList.size() > position && position >= 0) {
            String statusNow = this.dataList.get(position).getPano_status();
            if (!(this.dataList.get(position).getPano_status().equals("未完成") && status.equals("完成拍摄")))
                this.dataList.get(position).setPano_status(status);
            this.notifyItemChanged(position, 1);
        }
    }

    @NonNull
    @Override
    public PanoTaskHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.pano_item, null);
        return new PanoTaskHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull PanoTaskHolder holder, int position) {
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull PanoTaskHolder holder, int position, @NotNull List<Object> playloads) {
        if (playloads.isEmpty()) {
            holder.pano_id.setText(dataList.get(position).getId());
            holder.pano_shoot_num.setText(dataList.get(position).getShoot_num() + "");
            holder.pano_progress.setText(String.format("%s ", dataList.get(position).getPano_progress()) + "%");
            holder.pano_progressbar.setProgress(dataList.get(position).getPano_progress());
            holder.save_progress.setText(String.format("%s ", dataList.get(position).getSave_progress()) + "%");
            holder.save_progressbar.setProgress(dataList.get(position).getSave_progress());
            holder.save_path.setText(dataList.get(position).getSave_path());
            holder.upload_progress.setText(String.format("%s ", dataList.get(position).getUpload_progress() * 100) + "%");
            holder.upload_progressbar.setProgress(dataList.get(position).getUpload_progress());
            holder.pano_status.setText(dataList.get(position).getPano_status());
            Log.d(TAG, String.format("position %s, onBindViewHolder: %s", position, dataList.toString()));
        } else {
            Log.d(TAG, "onBindViewHolder playloads size: " + playloads.size());
            holder.pano_id.setText(dataList.get(position).getId());
            holder.pano_shoot_num.setText(dataList.get(position).getShoot_num() + "");
            holder.pano_progress.setText(String.format("%s ", dataList.get(position).getPano_progress()) + "%");
            holder.pano_progressbar.setProgress(dataList.get(position).getPano_progress());
            holder.save_progress.setText(String.format("%s ", dataList.get(position).getSave_progress()) + "%");
            holder.save_progressbar.setProgress(dataList.get(position).getSave_progress());
            holder.save_path.setText(dataList.get(position).getSave_path());
            holder.upload_progress.setText(String.format("%s ", dataList.get(position).getUpload_progress() * 100) + "%");
            holder.upload_progressbar.setProgress(dataList.get(position).getUpload_progress());
            holder.pano_status.setText(dataList.get(position).getPano_status());
            Log.d(TAG, String.format("position %s, onBindViewHolder: %s", position, dataList.toString()));
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    static class PanoTaskHolder extends RecyclerView.ViewHolder {
        private TextView pano_id;
        private TextView pano_shoot_num;
        private TextView pano_progress;
        private ProgressBar pano_progressbar;
        private TextView save_progress;
        private ProgressBar save_progressbar;
        private TextView save_path;
        private TextView upload_progress;
        private ProgressBar upload_progressbar;
        private TextView pano_status;

        PanoTaskHolder(@NonNull View itemView) {
            super(itemView);
            pano_id = itemView.findViewById(R.id.pano_id);
            pano_shoot_num = itemView.findViewById(R.id.pano_shoot_count);
            pano_progress = itemView.findViewById(R.id.pano_progress);
            pano_progressbar = itemView.findViewById(R.id.pano_progressbar);
            save_progress = itemView.findViewById(R.id.save_progress);
            save_progressbar = itemView.findViewById(R.id.save_progressbar);
            save_path = itemView.findViewById(R.id.save_path);
            upload_progress = itemView.findViewById(R.id.upload_progress);
            upload_progressbar = itemView.findViewById(R.id.upload_progressbar);
            pano_status = itemView.findViewById(R.id.pano_status);
        }
    }
}
